# -*- coding: utf-8 -*-
from hackprofile.models import Hacker, ForumProfile
from hackprofile.forms import EditContactsForm, EditInfoForm, \
                             ForumSettingsForm
from django.views.generic.edit import UpdateView
from django.core.urlresolvers import reverse
from django.contrib import messages
from django.contrib.auth.decorators import login_required, permission_required
from django.utils.decorators import method_decorator
from django.shortcuts import get_object_or_404

class AbstractEditProfile(UpdateView):

    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        self.queryset = self.model.objects.filter(user=request.user)
        return super(AbstractEditProfile, self).dispatch(request, \
                                                        *args, **kwargs)

    def get_object(self, queryset=None):
        obj = self.queryset.get()
        return obj   

    def form_valid(self, form):
        messages.success(self.request, u'Данные профиля были изменены')
        return super(AbstractEditProfile, self).form_valid(form)

    def get_success_url(self):
        return reverse(self.success_view_name, args=[])


class EditContacts(AbstractEditProfile):
    form_class = EditContactsForm
    template_name = 'edit_contacts.html'
    success_view_name = 'edit-contacts'
    model = Hacker


class EditInfo(AbstractEditProfile):
    form_class = EditInfoForm
    template_name = 'edit_info.html'
    success_view_name = 'edit-info'
    model = Hacker

class EditForumProfile(AbstractEditProfile):
    form_class = ForumSettingsForm
    template_name = 'edit_forum.html'
    success_view_name = 'edit-forum-profile'
    model = ForumProfile



class AbstractModerateProfile(UpdateView):
    
    @method_decorator(permission_required('hackprofile.can_moderate_profile'))
    def dispatch(self, request, *args, **kwargs):
        user = self.kwargs.get('user_id', request.user)
        self.queryset = self.model.objects.filter(user=user)
        return super(AbstractModerateProfile, self).dispatch(request, \
                                                            *args, **kwargs)   

    def get_object(self, queryset=None):
        self.user = self.queryset.get()
        return get_object_or_404(self.model.objects, user=self.user)
    
    def form_valid(self, form):
        message.success(self.request, u'Данные профиля были изменены')
        return super(AbstractModerateProfile, self).form_valid(*args, **kwargs)

    def get_success_url(self):
        return reverse(self.success_view_name, args=self.user)


    
class ModerateContacts(AbstractModerateProfile):
    form_class = EditContactsForm
    template_name = 'moderate_contacts.html'
    success_view_name = 'moderate-contacts'
    model = Hacker


class ModerateInfo(AbstractModerateProfile):
    form_class = EditInfoForm
    template_name = 'moderate_info.html'
    success_view_name = 'moderate-info'
    model = Hacker

class ModerateForumProfile(AbstractModerateProfile):
    form_class = ForumSettingsForm
    template_name = 'moderate_forum.html'
    success_view_name = 'moderate-forum-profile'
    model = ForumProfile
    
